package com.jerry.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

/**
 * Created by Shirly on 2016/12/12.
 */
public class test7 {
    public static void main(String[] args) {
        try {
            Class a =Class.forName("com.jerry.reflect.Box");
            Object o = a.newInstance();
            Field[] fields = a.getDeclaredFields();
            Method[] methods = a.getDeclaredMethods();
            StringBuffer sb = new StringBuffer();
            sb.append(Modifier.toString(a.getModifiers())+ " class "+a.getSimpleName()+"{\n");
            for(Field field :fields){
                sb.append(Modifier.toString(field.getModifiers())+" ");
                sb.append(field.getType().getSimpleName()+" ");
                sb.append(field.getName()+";\n");
            }
            sb.append("}");
            System.out.println(sb.toString());

            for(Method method : methods){
                System.out.println("方法的返回值类型为："+method.getReturnType());
                System.out.println("方法的参数值为：" + Arrays.toString(method.getParameterTypes()));
                System.out.println("方法的属性为："+Modifier.toString(method.getModifiers()));
            }
//            Box box = new Box();
            Method useMethod = a.getDeclaredMethod("useVoid",String.class);
            useMethod.invoke(o,"笑话");
            Method useString = a.getDeclaredMethod("useString",String.class);
            Object o1 = useString.invoke(o,"小朋友");
            System.out.println(o1.toString());

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}
